#include "devicelistdialog.h"
#include "ui_devicelistdialog.h"

DeviceListDialog::DeviceListDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::DeviceListDialog)
{
    ui->setupUi(this);

    ui->table_device->setEditTriggers(QAbstractItemView::NoEditTriggers);          // 禁止编辑
    ui->table_device->setSelectionBehavior(QAbstractItemView::SelectRows);         // 整行选中的方式
    ui->table_device->setSelectionMode(QAbstractItemView::SingleSelection);        // 选中模式为单行选中
    ui->table_device->setFocusPolicy(Qt::NoFocus);                                 // 不显示选中虚线
    ui->table_device->verticalHeader()->hide();                                    // 隐藏垂直表头
    ui->table_device->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->table_device->horizontalHeader()->setMinimumHeight(36);                    // 表头高度
    ui->table_device->horizontalHeader()->setStretchLastSection(true);             // 最后一行自适应表格
    ui->table_device->horizontalHeader()->setHighlightSections(false);             // 点击表头时不对表头光亮
    ui->table_device->setFont(qApp->font());
}

DeviceListDialog::~DeviceListDialog()
{
    delete ui;
}

void DeviceListDialog::setDeviceList(const QVariantList &deviceList)
{
    for (int i = 0; i < deviceList.count(); i++)
    {
        QVariantMap info = deviceList.at(i).toMap();
        ui->table_device->insertRow(i);
        QTableWidgetItem *header = new QTableWidgetItem(info.value("brand").toString());
        header->setData(999, info);
        ui->table_device->setItem(i, 0, header);
        ui->table_device->setItem(i, 1, new QTableWidgetItem(info.value("model").toString()));
        ui->table_device->setItem(i, 2, new QTableWidgetItem(info.value("name").toString()));
        ui->table_device->setItem(i, 3, new QTableWidgetItem(info.value("sn").toString()));
    }
}

const QVariantMap &DeviceListDialog::selectedDevice() const
{
    return m_selectedDevice;
}

void DeviceListDialog::on_table_device_cellDoubleClicked(int row, int )
{
    QTableWidgetItem *item = ui->table_device->item(row, 0);
    if (!item)
    {
        return;
    }
    m_selectedDevice = item->data(999).toMap();

    this->close();
    this->setResult(QDialog::Accepted);
}



